"use strict";
const superagent = require('superagent');
const moment = require('moment');
const retUtil = require('../routes/v1/utils/commonUtils/retUtil');

let TEST_DOMAIN = 'https://localhost:3014';

const authorizeUniTest = (function () {

    function authorizeUniTest() {

    }
    /*
    * 设置当前测试用例执行的环境
    * */
    authorizeUniTest.setTestDomain = function (domain) {
        TEST_DOMAIN = domain;
    };


    /**
     * 获取视频模板详情
     *
     * @author mk
     * @date 2018-06-04
     *
     * @param {Object} param   测试参数
     * @param {String} param.api_key  apikey
     * @param {String} param.nonce_str 随机字符串
     * @param {Number} param.expiration  过期时间的时间戳
     * @param {String} param.signature 通过apiKey,secret,nonce_str,expiration计算的到的签名
     * @param {Function} callback
     */
    authorizeUniTest.applyToken = function (param,callback) {
        let url = TEST_DOMAIN + '/v1/authorize/applyToken';
        let para = {
            api_key : param.api_key,
            nonce_str : param.nonce_str,
            expiration : param.expiration,
            signature : param.signature
        };
        superagent.post(url)
            .send(para)
            .end(function (error, result) {
                if (error || !result) {
                    if(callback) callback(error || '请求失败');
                    return;
                }
                let response = JSON.parse(result.text);
                if (!response || Number(response.code) !== 10000) {
                    if (callback) callback(response.msg || '请求失败');
                    return
                }
                if (callback) callback(null, response.data.token);
            });
    };

    /**
     * 查询token
     *
     * @author mk
     * @date 2018-06-05
     *
     * @param {Object} param   测试参数
     * @param {String} param.api_key  要查token的key
     * @param {String} param.token    需要查询的token
     * @param {Function} callback
     */
    authorizeUniTest.queryToken = function (param, callback) {
        let url = TEST_DOMAIN + '/v1/authorize/queryToken';
        let para = {
            api_key : param.api_key,
            token : param.token,
        };
        superagent.get(url)
            .send(para)
            .end(function (error, result) {
                if (error || !result) {
                    if(callback) callback(error || '请求失败');
                    return;
                }
                let response = JSON.parse(result.text);
                if (!response || Number(response.code) !== 10000) {
                    if (callback) callback(response.msg || '请求失败');
                    return;
                }
               return  callback(null, response.data);
            });
    };

    /**
     * 正确的参数
     */
    authorizeUniTest.AuthorizeTestCase1 = function(callback){
        let param = {
            api_key:"YJX3SG7012EQ90VI",
            nonce_str :"fdsdsfdsf",
            expiration:1530288000,
            signature:"75a21282497b3a3fa922fd15a29c04ca620eff9f"
        };
        //申请token
        authorizeUniTest.applyToken(param,function(err,data){
            let param2 = {
                api_key : param.api_key,
                token : data,
            };
            //查询token
            authorizeUniTest.queryToken(param2, function (error,data) {
                return callback(error,JSON.stringify(data));
            });
        });
    };
    /**
     * 错误的参数
     */
    authorizeUniTest.AuthorizeTestCase2 = function (callback) {
        //参数错误
        let param = {
            api_key:"YJX3SG7012EQ90VI",
            nonce_str :"dsfdsfdsdsd",
            expiration:1530288000,
            signature:"22222"
        };
        authorizeUniTest.applyToken(param,function (err,data) {
            let param2 = {
                api_key : param.api_key,
                token : data || "",
            };
            //查询token
            authorizeUniTest.queryToken(param2, function (error,data) {
                return callback(error,JSON.stringify(data));
            });
        });
    };

    return authorizeUniTest;
})();
module.exports = authorizeUniTest;



